SET(TARGET_NAME PluginDefault)
#PROJECT(${TARGET_NAME})
#必须要有的宏定义
ADD_DEFINITIONS (-DPLUGIN_DEFAULT_EXPORT)

IF (WIN32 )
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8")
    ADD_DEFINITIONS (-D_CRT_SECURE_NO_WARNINGS)
endif()
# ADD_DEFINITIONS (-DUSE_QT_BASIC)

	INCLUDE_DIRECTORIES( ${PROJECT_SOURCE_DIR}/${TARGET_NAME})
    INCLUDE_DIRECTORIES( ${PROJECT_SOURCE_DIR}/LibCommon)
	LINK_DIRECTORIES( ${PROJECT_BINARY_DIR}/lib)
    LINK_DIRECTORIES( ${PROJECT_BINARY_DIR}/LibCommon)

    #some third-party library
    # INCLUDE_DIRECTORIES(${OSG_INCLUDE_DIR})
    # LINK_DIRECTORIES(${OSG_LIB_DIR})

#库文件在debug下名字后面加d
SET(CMAKE_DEBUG_POSTFIX "d" CACHE STRING "add a postfix, usually d on windows")
SET(CMAKE_RELEASE_POSTFIX "" CACHE STRING "add a postfix, usually empty on windows")
#ADD_DEFINITIONS (${Qt5Widgets_DEFINITIONS})

FILE(GLOB_RECURSE ThisTarget_INCLUDE_LIST "${PROJECT_SOURCE_DIR}/${TARGET_NAME}/*.h")
FILE(GLOB_RECURSE ThisTarget_SRC_LIST "${PROJECT_SOURCE_DIR}/${TARGET_NAME}/*.cpp")

SET(RELFECT_INCLUDE_LIST
    ${PROJECT_SOURCE_DIR}/${TARGET_NAME}/EItemInPlugin.h
    )

FILE(GLOB_RECURSE RELFECTINCLUDE_LIST "${PROJECT_SOURCE_DIR}/${TARGET_NAME}/*.h")

source_group("include" FILES ${ThisTarget_INCLUDE_LIST})
source_group("src" FILES ${ThisTarget_SRC_LIST})

#ADD_EXECUTABLE(${TARGET_NAME} ${ThisTarget_SRC_LIST} ${ThisTarget_INCLUDE_LIST} )

ADD_LIBRARY(${TARGET_NAME} SHARED ${ThisTarget_SRC_LIST} ${ThisTarget_INCLUDE_LIST})


# ADD_CUSTOM_COMMAND(
    # TARGET ${TARGET_NAME}
    # PRE_BUILD
    # COMMAND ${CMAKE_COMMAND} -E echo ${PROJECT_SOURCE_DIR}
    # VERBATIM
    # )

# ADD_CUSTOM_COMMAND(
    # TARGET ${TARGET_NAME}
    # PRE_BUILD
    # COMMAND ${CMAKE_COMMAND} -E echo ${ThisTarget_INCLUDE_LIST}
    # VERBATIM
    # )

#TODO: failed to run ExeReflection in CMake, please fix it
# foreach(str ${RELFECT_INCLUDE_LIST})
    # message(STATUS "Processing ${str}")
    # ADD_CUSTOM_COMMAND(
        # TARGET ${TARGET_NAME}
        # PRE_BUILD
        # COMMAND ${PROJECT_SOURCE_DIR}/build_gcc/bin/ExeReflection -f ${str} -i ${PROJECT_SOURCE_DIR}/LibCommon -o ${PROJECT_SOURCE_DIR}/${TARGET_NAME}/Autogen
        # VERBATIM
        # )
# endforeach()



SET(ADDITIONAL_LIBRARIES_RELEASE
     LibCommon
)

SET(ADDITIONAL_LIBRARIES_DEBUG
     LibCommond
)


TARGET_LINK_LIBRARIES(${TARGET_NAME} PRIVATE debug "${ADDITIONAL_LIBRARIES_DEBUG}" PRIVATE optimized "${ADDITIONAL_LIBRARIES_RELEASE}")
set_target_properties(${TARGET_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG ${PROJECT_BINARY_DIR}/bin/plugin)
set_target_properties(${TARGET_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${PROJECT_BINARY_DIR}/bin/plugin)
set_target_properties(${TARGET_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO ${PROJECT_BINARY_DIR}/bin/plugin)

#set_target_properties(${TARGET_NAME} PROPERTIES LINK_FLAGS_RELEASE "/SUBSYSTEM:CONSOLE /ENTRY:mainCRTStartup")# WINDOWS

set_target_properties(${TARGET_NAME} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${PROJECT_BINARY_DIR}/lib/plugin)
set_target_properties(${TARGET_NAME} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${PROJECT_BINARY_DIR}/lib/plugin)

#=============Copy Plugins Runtime FILES to Main Project============ 
#一般将此内容放在lib子项目的CMakelists.txt的最后，该方法采用POST_BUILD，
#所以需注意要复制的源应该是一个固定字符串，
#而不能用FILE GLOB的方法，因为在编译前该源为空，在VS中会出现MSB3073错误，提示copy from 为空值。

SET(DLL_FILE_DEBUG
    ${PROJECT_BINARY_DIR}/bin/plugin/${TARGET_NAME}d.dll 
) 

SET(DLL_FILE_RELEASE 
    ${PROJECT_BINARY_DIR}/bin/plugin/${TARGET_NAME}.dll 
) 

# add_custom_command(TARGET ${TARGET_NAME} 
    # PRE_BUILD 
    # COMMAND ../bin/ExeReflection.exe 
        # -f "${PROJECT_SOURCE_DIR}/EItemXmlRoot.h"
        # -i "${PROJECT_SOURCE_DIR}/PluginXml;${PROJECT_SOURCE_DIR}/LibUiQt;${PROJECT_SOURCE_DIR}/LibCommon"
        # -o "${PROJECT_SOURCE_DIR}/PluginXml/Autogen"
# ) 

add_custom_command(TARGET ${TARGET_NAME} 
    PRE_BUILD 
    COMMAND echo HELLO
) 

